//
//  TODrawerViewController.m
//  B段项目
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "TODrawerViewController.h"
#import "TODrawerTableViewCell.h"
#import "TOHeaderView.h"
#import "TOFooterView.h"
#import "TOSearchViewController.h"
#import "HLJSeletionViewController.h"
#import "ZZCGreatViewController.h"
#import "TORadioViewController.h"
#import "ZZCReadViewController.h"
#import "HLJCommunityViewController.h"
#import "ZZCDynamicViewController.h"
#import "ZZCSettingViewController.h"
#import "ZZCPlayListModel.h"

@interface TODrawerViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *menuArr;
@property(nonatomic,strong)NSMutableArray *logoArr;
@property(nonatomic,strong)TOFooterView *footerView;
@property(nonatomic,strong)NSIndexPath *oldIndexPath;


@end

@implementation TODrawerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
    [self createTableView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(musicStop) name:@"stop" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(musicPlay) name:@"play" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(musicInfo:) name:@"setData" object:nil];
}

#pragma mark - 播放界面发来通知的方法
-(void)musicStop
{
    [self.footerView.playBtn setImage:[UIImage imageNamed:@"DWbofang1.png"] forState:UIControlStateNormal];
}
-(void)musicPlay
{
    [self.footerView.playBtn setImage:[UIImage imageNamed:@"iconfont-zanting-3.png"] forState:UIControlStateNormal];

}
-(void)musicInfo:(NSNotification *)notifition
{
    [self.footerView.playBtn setImage:[UIImage imageNamed:@"iconfont-zanting-3.png"] forState:UIControlStateNormal];
    ZZCPlayListModel *model = notifition.object;
    [self.footerView.specialImageView sd_setImageWithURL:model.imgUrl placeholderImage:[UIImage imageNamed:@""]];
    self.footerView.titleLabel.text = model.title;
    self.footerView.subTitle.text = [model.authorinfo objectForKey:@"uname"];
}

#pragma mark - 创建TableView
-(void)createTableView
{
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStyleGrouped];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    
    //去掉cell的线
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //注册cell
    [self.myTableView registerClass:[TODrawerTableViewCell class] forCellReuseIdentifier:@"TODrawerTableViewCell"];
    
    
#pragma mark 设置头视图 && 底视图
    
    //底视图
    self.myTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/7)];
    self.myTableView.tableFooterView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
    
    //头视图
    self.myTableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT*0.3)];
    self.myTableView.tableHeaderView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
    
#pragma mark 添加头视图 && 底视图 View
    
    //设置头视图View
    self.headerView = [[TOHeaderView alloc]init];
    self.headerView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
    [self.view addSubview:self.headerView];

    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(self.myTableView.tableHeaderView);
    }];
    [self.headerView.search addTarget:self action:@selector(searchBtn) forControlEvents:UIControlEventTouchUpInside];
   
    //设置底视图
    self.footerView = [[TOFooterView alloc] init];
    self.footerView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.footerView];
    [self.footerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.bottom.width.equalTo(self.view);
        make.height.mas_equalTo(HEIGHT/7);
    }];
    
#pragma mark  给FootView添加轻拍手势
    UITapGestureRecognizer *footViewTGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(footViewAction:)];
    footViewTGR.numberOfTapsRequired = 1;
    //打开用户交互
    self.footerView.userInteractionEnabled = YES;
    [self.footerView addGestureRecognizer:footViewTGR];
    
#pragma mark 菜单数组
    
    //菜单名字数组
    self.menuArr = [NSMutableArray arrayWithObjects:@"首页",@"电台",@"阅读",@"社区",@"碎片",@"良品",@"设置", nil];
    //菜单logo数组
    self.logoArr = [NSMutableArray arrayWithObjects:@"iconfont-tubiao15.png", @"iconfont-yuleyinlediantai-2.png", @"iconfont-shu.png", @"iconfont-guanyuwomenicon.png", @"iconfont-pengyouwang.png", @"iconfont-shangpin.png", @"iconfont-shezhi副本.png", nil];
}

#pragma mark -  FootView轻拍方法(push至播放器界面)
- (void)footViewAction:(UIRotationGestureRecognizer *)footViewAction{
    
    AppDelegate *pushToVC = (AppDelegate *)[[UIApplication sharedApplication]delegate];
    
    if ([[ZZCMusicPlayerViewController shareMusicPlayerViewController] pushed]) {
        
        [pushToVC.homeNC popToViewController:[ZZCMusicPlayerViewController shareMusicPlayerViewController] animated:YES];
    }else{
    
        [pushToVC.homeNC pushViewController:[ZZCMusicPlayerViewController shareMusicPlayerViewController] animated:YES];
        [[ZZCMusicPlayerViewController shareMusicPlayerViewController] setPushed:YES];
    }


    //关闭抽屉
    [pushToVC.leftSliderVC closeLeftView];
    
    
    
}


#pragma mark - 设置行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.menuArr.count;
}

#pragma mark - 设置cell  赋值
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //从重用池中取出标记后的cell
    TODrawerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TODrawerTableViewCell"];
    
    
    //赋值
    cell.menuLabel.text = [self.menuArr objectAtIndex:indexPath.row];
    cell.logoImageView.image = [UIImage imageNamed:[self.logoArr objectAtIndex:indexPath.row]];
    
    //设置cell 背景颜色
    cell.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];;
    
    return cell;
}




#pragma mark - 设置cell高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return HEIGHT/15+10;
}

#pragma mark - cell点击跳转方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

#pragma mark 设置点击改变图片和label颜色
    //取当前cell下标
    TODrawerTableViewCell *cell = (TODrawerTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    //触摸时显示的 logo图片数组
    NSMutableArray *touchImageArray = [NSMutableArray arrayWithObjects:@"iconfont-tubiao15-2.png", @"iconfont-yuleyinlediantai-3.png", @"iconfont-shu-2.png", @"iconfont-guanyuwomenicon-2.png", @"iconfont-pengyouwang-2.png", @"iconfont-shangpin-2.png", @"iconfont-shezhi-2.png", nil];
    
    if (self.oldIndexPath) {
        //把之前点过的cell 变回原来的图标和字体颜色
        cell = [tableView cellForRowAtIndexPath:self.oldIndexPath];
        [cell.menuLabel setTextColor:[UIColor colorWithRed:0.57 green:0.57 blue:0.57 alpha:1]];
        cell.logoImageView.image = [UIImage imageNamed:[self.logoArr objectAtIndex:self.oldIndexPath.row]];
        
        //把正在选中的cell  赋值白色的图标和字体颜色
        cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell.menuLabel setTextColor:[UIColor whiteColor]];
        cell.logoImageView.image = [UIImage imageNamed:[touchImageArray objectAtIndex:indexPath.row]];
        
    }else{
        //把正在选中的cell  赋值白色的图标和字体颜色
        [cell.menuLabel setTextColor:[UIColor whiteColor]];
        cell.logoImageView.image = [UIImage imageNamed:[touchImageArray objectAtIndex:indexPath.row]];
        
    }
    
    //重新记录
    self.oldIndexPath = indexPath;
    
#pragma mark 点击Cell时页面跳转
    
    //首页
    if (indexPath.row == 0) {
        
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [tempAppDelegate.leftSliderVC closeLeftView];//关闭左侧抽屉
        
        //跳转页面
        HLJSeletionViewController *homeVC = [[HLJSeletionViewController alloc]init];
        [tempAppDelegate.homeNC pushViewController:homeVC animated:NO];
        
    }
    
    //电台
    if (indexPath.row == 1) {
        
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [tempAppDelegate.leftSliderVC closeLeftView];//关闭左侧抽屉
        
        //跳转页面
        TORadioViewController *radioVC = [[TORadioViewController alloc]init];
        [tempAppDelegate.homeNC pushViewController:radioVC animated:NO];
    }
    //阅读
    if (indexPath.row == 2) {
        
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [tempAppDelegate.leftSliderVC closeLeftView];//关闭左侧抽屉
        
        //跳转页面
        ZZCReadViewController *readVC = [[ZZCReadViewController alloc]init];
        [tempAppDelegate.homeNC pushViewController:readVC animated:NO];
    }
    //社区
    if (indexPath.row == 3) {
        
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [tempAppDelegate.leftSliderVC closeLeftView];//关闭左侧抽屉
        
        //跳转页面
        HLJCommunityViewController *communityVC = [[HLJCommunityViewController alloc]init];
        [tempAppDelegate.homeNC pushViewController:communityVC animated:NO];
    }
    
    //碎片
    
    if (indexPath.row == 4) {
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [tempAppDelegate.leftSliderVC closeLeftView];//关闭左侧抽屉
        
        ZZCDynamicViewController *dynamicVC = [[ZZCDynamicViewController alloc]init];
        [tempAppDelegate.homeNC pushViewController:dynamicVC animated:NO];
        
    }
    //良品
    
    if (indexPath.row == 5) {
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [tempAppDelegate.leftSliderVC closeLeftView];//关闭左侧抽屉
        
        ZZCGreatViewController *shopVC = [[ZZCGreatViewController alloc]init];
        [tempAppDelegate.homeNC pushViewController:shopVC animated:NO];
        
    }if (indexPath.row == 6) {
        
        AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [tempAppDelegate.leftSliderVC closeLeftView];//关闭左侧抽屉
        
        ZZCSettingViewController *settVC = [[ZZCSettingViewController alloc]init];
        [tempAppDelegate.homeNC pushViewController:settVC animated:NO];
        
    }

    
}

#pragma mark - 搜索按钮方法
-(void)searchBtn{
    
    
    
    TOSearchViewController *searchVC = [[TOSearchViewController alloc]init];
    
    UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:searchVC];
    naVC.navigationBar.translucent = NO;
    [self presentViewController:naVC animated:YES completion:^{
        
        
    }];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
